// chart是d3初始化之后的实例，不直接放到函数里是防止多次初始化造成性能浪费
export default (dom, domBg, drawData) => {
  const myChartcircle = window.ThsDataVStandardChart.init(dom);
  const st = dom.offsetHeight * 0.16;
  let sd = st * 2;
  domBg.style.width = `${st * 2}px`;
  domBg.style.height = `${st * 2}px`;
  domBg.style.marginTop = `${-st}px`;
  domBg.style.marginLeft = `${-st}px`;

  const dt = [];
  const colorList = ['#6E61FF', '#42D780', '#58B9FF', '#5AEEE2', '#FF5866 ', '#FF8A50', '#FFAE3D ', '#C56EE2 '];
  function putData(x, y, colorItem) {
    //分子 分母 颜色colorList

    for (let i = 0; i < y; i++) {
      dt.push({
        value: x / y,
        itemStyle: {
          normal: {
            color: colorItem,
            borderColor: colorItem,
          },
          emphasis: {
            color: colorItem,
            borderColor: colorItem,
          },
        },
        label: {
          normal: {
            show: false,
          },
          emphasis: {
            show: false,
          },
        },
      });
    }
  }
  const disArr = [];
  let max = 0;
  function put(n) {
    putData(drawData[n][0], drawData[n][1], colorList[n]);
    disArr.push(drawData[n][0] / drawData[n][1]);
  }

  for (let i = 0; i < drawData.length; i++) {
    put(i);
  }
  max = Math.max(...disArr.slice(0,7));
  dt.forEach(item => {
    if (+item.value === 0) {
      item.value = 0.025;
    }
  });
  if (+max === 0) {
    max = 0.025;
  }
  sd = st * (1 - max) + sd * max;
  const option = {
    radiusAxis: {
      show: false,
    },
    legendHoverLink: false,
    series: [
      {
        type: 'pie',
        radius: [st, sd],
        center: ['50%', '50%'],
        roseType: 'area',
        hoverAnimation: false,
        avoidLabelOverlap: false,
        clickable: false,
        animation: false,
        data: dt,
        itemStyle: {
          normal: {
            color: function (params) {
              return colorList[params.dataIndex];
            },
          },
        },
        label: {
          normal: {
            show: false,
          },
        },
        labelLine: {
          normal: {
            show: false,
          },
        },
      },
    ],
    animationDuration: 300,
  };
  myChartcircle.play({ option });
};
